import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paly_android/bloc/main_bloc.dart';
import 'package:paly_android/bloc/mian_page.dart';

class MainPage extends StatefulWidget {
  MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() {
    return _MainPageState();
  }
}

class _MainPageState extends State<MainPage> {

  var bottomItem = [
    ["首页", Icons.home],
    ["项目", Icons.work],
    ["公众号", Icons.code],
    ["体系", Icons.list]
  ];

  List<BottomNavigationBarItem> _generateBottomItems() {
    List<BottomNavigationBarItem> items =
    List.generate(bottomItem.length, (index) {
      dynamic item = bottomItem[index];
      return BottomNavigationBarItem(label: item[0], icon: Icon(item[1]));
    });

    return items;
  }

  late MainBloc bloc;

  @override
  void initState() {
    super.initState();
    bloc = MainBloc(0);
    bloc.changePage(0);
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return homeWidget(context);
  }

  Widget homeWidget(BuildContext context) {
    return BlocProvider(create: (context) =>bloc,
        child: Scaffold(
            body: MianPage(),
            bottomNavigationBar: BlocBuilder<MainBloc, int>(
              builder: (context, state) {
                return BottomNavigationBar(
                  currentIndex: state,
                  selectedItemColor: Colors.blue,
                  unselectedItemColor: Colors.black87,
                  showUnselectedLabels: true,
                  type: BottomNavigationBarType.fixed,
                  items: _generateBottomItems(),
                  onTap: (index) {
                    BlocProvider.of<MainBloc>(context).changePage(index);
                  },
                );
              },
            )));
  }

}